#include "GameStateManager.h"
#include "GameState.h"

GameStateManager::GameStateManager(GameState *state,sf::RenderWindow* window)
:m_quit(false)
,window(window)
{
	pushState(state);
	//states.back()->init(window);

}

GameStateManager::~GameStateManager()
{
	//Clear States Vector
	while(!states.empty())
	{
		states.back()->cleanup();
		states.pop_back();
	}
}

void GameStateManager::changeState(GameState *state)
{
	if(!states.empty())
	{
		states.back()->cleanup();
		states.pop_back();
	}

	// store and init the new state
	states.push_back(state);
	states.back()->init(window);
}

void GameStateManager::pushState(GameState *state)
{
	//Pause the current state
	if(!states.empty())
		states.back()->pause();

	// store and init the new state
	states.push_back(state);
	states.back()->init(window);
}

void GameStateManager::popState()
{
	//If there are states, we delete the last one
	if(!states.empty())
	{
		states.back()->cleanup();
		states.pop_back();
	}

	//If there are states left, we resume the last of it
	//If no, we quit
	if(!states.empty())
		states.back()->resume();
	else m_quit = true;
	
}

void GameStateManager::quit()
{
	m_quit = true;
}

void GameStateManager::update()
{
	while(!m_quit)
		states.back()->update();
		
}

void GameStateManager::draw()
{
	while(!m_quit)
		states.back()->update();
		
}